﻿using RStudio.UIFramwork.Utils;
using System;
using UnityEngine;


namespace RStudio.UIFramwork
{
    /// <summary>
    /// 处理器|单例唯一性
    /// </summary>
    public class HandlerUseAsSingle : IUIHandler, IUIAcquisition
    {
        public IUIHandler Next { get; set; }

        public void Handle(UIBase uiBase)
        {
            if (uiBase.UseAsSingle)
            {
                if (IsAcquisition)
                {
                    if (string.IsNullOrEmpty(uiBase.UID))
                    {
                        uiBase.UID = Guid.NewGuid().ToString();
                    }

                    // 单例不更新UID
                    LayoutUtil.SetSibling(uiBase.transform, -1);
                }
                else
                {
                    uiBase.UID = null;
                }
            }
            else
            {
                // 非单例更新UID
                uiBase.UID = Guid.NewGuid().ToString();
            }

            Next?.Handle(uiBase);
        }



        public bool IsAcquisition { get; private set; }
        public HandlerUseAsSingle(bool isAcquisition = true)
        {
            this.IsAcquisition = isAcquisition;
        }
    }
}
